package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.OperaDetaltisEntity;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.utils.Okhttputils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/15.
 */

public class OperateDelatis extends BaseActivity {

    private String orderid1;
    private ImageView imageView;
    private ListView list;
    private List<OperaDetaltisEntity.DataBean> listdate = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.operalayout);
        Intent intent = getIntent();
        orderid1 = intent.getStringExtra("orderid");

        init();
        network();
    }

    private void network() {
        Map<String, String> headerParams = RequestParam.getHeaderParams();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("orderid", orderid1);
        Okhttputils.Instanse(this).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + "/orderhandle/pushlistdetatil", stringStringHashMap, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                final OperaDetaltisEntity operaDetaltisEntity = (OperaDetaltisEntity) o;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listdate = operaDetaltisEntity.getData();
                        list.setAdapter(new Myadapter());
                    }
                });
            }
        }, OperaDetaltisEntity.class);
    }

    private void init() {
//        orderid = (TextView) findViewById(R.id.orderid);
//        name = (TextView) findViewById(R.id.name);
//        buyNum = (TextView) findViewById(R.id.buyNum);
//        other = (TextView) findViewById(R.id.other);
        imageView = (ImageView) findViewById(R.id.iv_back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        list = (ListView) findViewById(R.id.list);

    }

    class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            return listdate.size();
        }

        @Override
        public Object getItem(int position) {
            return listdate.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(OperateDelatis.this).inflate(R.layout.operalistitem, null);
                viewHolder.orderid = (TextView) convertView.findViewById(R.id.orderid);
                viewHolder.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder.buyNum = (TextView) convertView.findViewById(R.id.buyNum);
                viewHolder.other = (TextView) convertView.findViewById(R.id.other);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();

            }
            OperaDetaltisEntity.DataBean dataBean = listdate.get(position);
            String buyNum = dataBean.getBuyNum();
            String name = dataBean.getName();
            String orderid = dataBean.getOrderid();
            String other = dataBean.getOther();
            viewHolder.orderid.setText("订单号:" + orderid);
            viewHolder.buyNum.setText("数量:" + buyNum);
            viewHolder.name.setText("名字：" + name);
            viewHolder.other.setText("首单:" + other);
            return convertView;
        }

        class ViewHolder {

            TextView orderid;
            TextView name;
            TextView buyNum;
            TextView other;
        }
    }
}
